﻿// ===============================================================================
// XCI.NET 西安交通信息WinForm开发框架 2018 版权所有
// ===============================================================================
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Reflection;
using DevExpress.Utils;
using XCI.Core;
using XCI.Extensions;
using XCI.Windows;

namespace XCI.Setting.Controls
{
    [ToolboxItem(false)]
    public partial class SettingAppearance : BaseUserControl
    {
        //汉化字典
        private static readonly Dictionary<string, string> _descs = new Dictionary<string, string>();
        //外观对象类型
        private Type _appearanceType;
        //外观对象
        private BaseAppearanceCollection _appearanceObject;

        /// <summary>
        /// 构造函数
        /// </summary>
        public SettingAppearance()
        {
            InitializeComponent();
            InitDescs();
        }

        /// <summary>
        /// 初始化
        /// </summary>
        /// <param name="appearanceObject">外观对象</param>
        public void Init(BaseAppearanceCollection appearanceObject)
        {
            if (appearanceObject == null) return;
            this._appearanceObject = appearanceObject;
            this._appearanceType = appearanceObject.GetType();

            var data = new List<AppearanceCaption>();
            var pi = _appearanceType.GetProperties();
            foreach (PropertyInfo info in pi)
            {
                if (!info.PropertyType.Name.Equals("AppearanceObject")) continue;

                AppearanceCaption caption = new AppearanceCaption();
                string name = info.Name;
                string des = name;
                if (_descs.TryGetValue(name, out string val))
                {
                    des = val;
                }
                caption.Name = name;
                caption.Caption = des;
                caption.Object = info.GetValue(_appearanceObject);
                data.Add(caption);
            }
            gridControl.DataSource = data;
        }

        /// <summary>
        /// 初始化本地字典
        /// </summary>
        private void InitDescs()
        {
            if (_descs != null) return;
            _descs.Set("HeaderPanel", "页眉面板").Set("GroupPanel", "分组面板").Set("FooterPanel", "页脚面板")
                .Set("Row", "数据行").Set("TopNewRow", "新建行").Set("RowSeparator", "行分割")
                .Set("GroupRow", "分组行").Set("EvenRow", "偶数行").Set("OddRow", "奇数行")
                .Set("HorzLine", "水平线").Set("VertLine", "垂直线").Set("Preview", "预览")
                .Set("FocusedRow", "焦点行").Set("FocusedCell", "焦点列").Set("GroupButton", "分组按钮")
                .Set("DetailTip", "详细提示").Set("GroupFooter", "分组页脚面板").Set("Empty", "空白区")
                .Set("SelectedRow", "选中行").Set("HideSelectionRow", "失去焦点选中行").Set("ColumnFilterButton", "列过滤按钮")
                .Set("ColumnFilterButtonActive", "列过滤按钮激活").Set("FixedLine", "固定线")
                .Set("CustomizationFormHint", "自定义窗口提示").Set("FilterCloseButton", "查询关闭按钮")
                .Set("FilterPanel", "查询面板").Set("TreeLine", "树线条").Set("ViewCaption", "视图标题");
        }

        private void SetObject()
        {
            if (gridView1.GetRow(gridView1.FocusedRowHandle) is AppearanceCaption item)
            {
                propertyGridPlus.SelectedObject = item.Object;
                propertyGridPlus.Enabled = true;
            }
            else
            {
                propertyGridPlus.SelectedObject = null;
                propertyGridPlus.Enabled = false;
            }
        }

        private void gridView1_FocusedRowChanged(object sender, DevExpress.XtraGrid.Views.Base.FocusedRowChangedEventArgs e)
        {
            SetObject();
        }

        //private void gridView1_CustomDrawRowIndicator(object sender, RowIndicatorCustomDrawEventArgs e)
        //{
        //    if (gridView1.OptionsView.ShowIndicator && e.Info.IsRowIndicator && e.RowHandle >= 0)
        //    {
        //        e.Info.DisplayText = (e.RowHandle + 1).ToString();
        //        e.Info.ImageIndex = -1;
        //    }
        //}
    }

    public class AppearanceCaption : NameCaption
    {
        public object Object { get; set; }
    }
}
